#include "textitem.h"

#include "AEE.h"
#include "AEEStaticWidget.h"
#include "AEEImageWidget.h"
#include "AEEPropContainer.h"
#include "dfon_comdef.h"
#include "node.h"
#include "AEEVectorModel.h"

typedef struct TextItem
{
	IPropContainer *propContainer;
	IWidget *text;
	IWidget *number;
	IModel *model;
	ModelListener mdlListener;
	HandlerDesc hdesc;
	IVectorModel *numberedImages;
} TextItem ;

static void OnModelEvent(void *userData, ModelEvent *event)
{
	TextItem *me = userData;

	if(event->evCode == EVT_MDL_VALUE)
	{
		NodeGeneric *node = IVALUEMODEL_GetValue((IValueModel*)event->pModel, NULL);
		if(node){
			IWIDGET_SetText(me->text, node->nodeName, FALSE);
			if(me->numberedImages)
			{
				IImage *numberImage = NULL;
				IVECTORMODEL_GetAt(me->numberedImages, node->index, (void**)&numberImage);
				IWIDGET_SetImage(me->number, numberImage);
			}
		}
	}
}

static void TextItem_Free(void *data)
{
	TextItem *me = data;
	LISTENER_Cancel(&me->mdlListener);
	RELEASEIF(me->numberedImages);
	RELEASEIF(me->model);
	RELEASEIF(me->number);
	RELEASEIF(me->text);
	HANDLERDESC_Free(&me->hdesc);
	FREEIF(me);
}

static void TextItem_HandleEvent(void *data, AEEEvent evt, uint16 wParam, uint32 dwParam)
{
	TextItem *me = data;

	if(evt == EVT_WDG_SETPROPERTY && wParam != PROP_BGIMAGE)
	{
		if(wParam == PROP_NUMBERED_IMAGES_VECTOR){
			me->numberedImages = (IVectorModel*)dwParam;
			ADDREFIF(me->numberedImages);
		}else{
			IWIDGET_HandleEvent(me->text, evt, wParam, dwParam);
		}
	}

	HANDLERDESC_Call(&me->hdesc, evt, wParam, dwParam);
}

int TextItem_New(IWidget** ppo, IShell *shell)
{
	WidgetProp propImage = {WIDGET_SIZE_TO_FIT, TRUE};
	WidgetProp propText = {1, TRUE};
	int retval = SUCCESS;
	TextItem *me = MALLOCREC(TextItem);
	IWidget *containerWidget = NULL;

	if(!me)
		return ENOMEMORY;

	ISHELL_CreateInstance(shell, AEECLSID_PROPCONTAINER, (void**)&me->propContainer);
	ISHELL_CreateInstance(shell, AEECLSID_VALUEMODEL, (void**)&me->model);
	ISHELL_CreateInstance(shell, AEECLSID_STATICWIDGET, (void**)&me->text);
	ISHELL_CreateInstance(shell, AEECLSID_IMAGEWIDGET, (void**)&me->number);
	if(!me->model || !me->propContainer || !me->text || !me->number)
	{
		retval = ENOMEMORY;
		goto cleanup;
	}
	IPROPCONTAINER_QueryInterface(me->propContainer, AEEIID_WIDGET, (void**)&containerWidget);

	IWIDGET_SetModel(containerWidget, me->model);
	HANDLERDESC_Init(&me->hdesc, TextItem_HandleEvent, me, TextItem_Free);
	IWIDGET_SetHandler(containerWidget, &me->hdesc);
	IMODEL_AddListenerEx(me->model, &me->mdlListener, OnModelEvent, me);
	IWIDGET_SetFlags(me->number, IDF_ALIGN_CENTER|IDF_ALIGN_MIDDLE);
	IWIDGET_SetLayoutStyle(containerWidget, LAYOUT_HORZ);
	IPROPCONTAINER_Insert(me->propContainer, me->number, WIDGET_ZNORMAL, &propImage);
	IPROPCONTAINER_Insert(me->propContainer, me->text, WIDGET_ZNORMAL, &propText);

	*ppo = containerWidget;

cleanup:
	if(retval != SUCCESS){
		TextItem_Free(me);
		RELEASEIF(me->propContainer);
	}
	RELEASEIF(containerWidget);
	return retval;
}